package com.itboye.ihomebank.homefragment;

import java.util.Calendar;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.ScriptGroup.Input;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.ZouShiBean;
import com.itboye.ihomebank.custom.ShiView;
import com.itboye.ihomebank.custom.ShiViews;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.WriteYanZhengMaDialog.OnfinishListener;
import com.itboye.ihomebank.util.WriteZhangHaoPopupWindow;

@SuppressLint("NewApi")
public class ActivityZouShi extends BaseOtherActivity implements Observer,
		OnfinishListener {
	private HorizontalScrollView hsv;
	private LinearLayout lay;
	private LinearLayout lays;
	private Context context;
	private static int[] temp = { 3, 16, 7, 25, 18, 12, 30 };
	private static int[] temps = { 3, 14, 4, 32, 14, 14, 0 };
	RelativeLayout top;
	TextView add_shap_title_tv;
	ImageView close_icon, chooseTime;
	private static String[] time = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六",
			"星期日" };
	TextView month, year, taoshu, pingjun;
	UserPresenter userPresenter;
	private String house_room = "";
	private int which = 11;
	private String chart_type = "month";
	private String community_id = "0";
	int width, height;
	TextView nian;
	private EditText pass;
	private WriteZhangHaoPopupWindow popupWindow;

	private void initDate() {
		community_id = getIntent().getStringExtra("community_id");
		userPresenter = new UserPresenter(this);
		userPresenter.zoushi(community_id, chart_type, which + "", house_room);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.month:
			chart_type = "month";
			userPresenter.zoushi(community_id, chart_type, which + "",
					house_room);

			month.setBackgroundColor(Color.parseColor("#DD8347"));
			year.setBackgroundColor(Color.parseColor("#B86229"));
			break;
		case R.id.year:
			chart_type = "year";
			userPresenter.zoushi(community_id, chart_type, "0", house_room);
			month.setBackgroundColor(Color.parseColor("#B86229"));
			year.setBackgroundColor(Color.parseColor("#DD8347"));
			break;
		case R.id.chooseTime:
			popupWindow = new WriteZhangHaoPopupWindow(this, this);
			pass = (EditText) popupWindow.getContentView().findViewById(
					R.id.pass);
			pass.setInputType(InputType.TYPE_CLASS_NUMBER);
			pass.setHint(chart_type.equals("year") ? "请输入年份" : "请输入月份");
			popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
			break;
		case R.id.btn_ok:
			String select = pass.getText().toString();
			if (select.equals("")) {
				ByAlert.alert("请输入有效数字");
				return;
			}
			if (chart_type.equals("year")) {
				nian.setText(select);
			}
			showProgressDialog("查询中,请稍后", false);
			userPresenter.zoushi(community_id, chart_type, select, house_room);

			popupWindow.dismiss();
			break;
		case R.id.btn_cancel:
			popupWindow.dismiss();
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_zoushi;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		context = this;
		which = Calendar.getInstance().get(Calendar.MONTH) + 1;
		nian.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
		top.setBackgroundColor(Color.parseColor("#DD8347"));

		add_shap_title_tv.setText("小区价格走势");
		add_shap_title_tv.setTextColor(Color.parseColor("#ffffff"));
		initDate();

	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.zoushi_success) {
				lay.removeAllViews();
				lays.removeAllViews();
				ZouShiBean bean = (ZouShiBean) entity.getData();
				List<Integer> listStr = bean.getChart();
				temp = new int[listStr.size()];
				for (int i = 0; i < listStr.size(); i++) {
					temp[i] = listStr.get(i);
				}
				time = new String[listStr.size()];
				if (chart_type.equals("month")) {
					width = 5000;
					for (int i = 0; i < time.length; i++) {
						time[i] = which + "-" + (i + 1);
					}
				} else if (chart_type.equals("year")) {

					width = 2500;
					for (int i = 0; i < time.length; i++) {
						time[i] = i + "月";
					}
				}
				ShiView ssvv = new ShiView(context, temp, temps, time);
				LayoutParams lpp = new LayoutParams(width, 600);
				ssvv.setLayoutParams(lpp);
				lay.addView(ssvv);
				ShiViews ssvvs = new ShiViews(context);
				LayoutParams lpps = new LayoutParams(width, 600);
				ssvvs.setLayoutParams(lpps);
				lays.addView(ssvvs);
				taoshu.setText(bean.getCount() + "套");
				pingjun.setText("￥" + bean.getCount());
			} else if (entity.getEventType() == UserPresenter.zoushi_fail) {
				ByAlert.alert(entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void getEditText(String edit) {
		// TODO Auto-generated method stub

	}

	@Override
	public void cancel() {
		// TODO Auto-generated method stub

	}
}
